<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

/**
 * App\Promotion
 *
 * @mixin \Eloquent
 */
class Promotion extends Model
{
    public $timestamps = false;
    protected $table = 's_order';


    //类型
    const ACT_TYPE_NULL = 0;
    const ACT_TYPE_ROB = 3;
    const ACT_TYPE_COLLAGE = 2;

    static $ACTTypeDisplay = [
        self::ACT_TYPE_NULL => '空',
        self::ACT_TYPE_ROB => '抢团',
        self::ACT_TYPE_COLLAGE => '拼团',
    ];

    //状态
    const STATUS_ON = 1;
    const STATUS_PAYING = 2;
    const STATUS_RECEIVING = 3;
    const STATUS_PING = 4;

    static $StatusDisplay = [
        self::STATUS_ON => '进行中',
        self::STATUS_PAYING => '待付款',
        self::STATUS_PING => '待评价',
        self::STATUS_RECEIVING => '待收货',
    ];

    //抢团状态
    const IS_WIN_ON = 1;
    const IS_WIN_NO = 2;
    const IS_WIN_YES = 3;

    static $IswinDisplay = [
        self::IS_WIN_ON => '已参与',
        self::IS_WIN_NO => '失败',
        self::IS_WIN_YES => '成功',
    ];


    //送货方式
    const DELIVERY_NULL = 0;
    const DELIVERY_MANUAL = 1;
    const DELIVERY_EXPRESS = 2;

    static $DeliveryDisplay = [
        self::DELIVERY_NULL => '未发货',
        self::DELIVERY_MANUAL => '人工配送',
        self::DELIVERY_EXPRESS => '快递',
    ];


    /**
     * 发货
     * @param $request
     * @return array
     */
    static function PromotionSend($request)
    {
        DB::beginTransaction();

        $order = Promotion::findOrFail($request->id);
        $order->type = self::STATUS_RECEIVING;
        $order->delivery = $request->delivery;
        $content = '您的 ' . $order->sp_title . ' 已发货,人工配送的哦';
        //填写快递名称和快递单号
        if (intval($request->delivery) === self::DELIVERY_EXPRESS) {
            $order->express_name = $request->express_name;
            $order->express_order = $request->express_order;
            $content = "亲，您的订单已由{$request->express_name}发出，单号{$request->express_order}，如有问题请联系客服【康侣】";
        }

        if (!$order->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '发货失败'];
        }
        //添加系统消息
        $user_id = DB::table('t_member')->where('uuid', $order->uuid)->value('c_id');

        $message = Curl::PushOneMessage($user_id, $content);
        if ($message['status'] !== 1) {
            DB::rollBack();

            return $message;
        }
//        if (!DB::table('jpush')->insert(
//            [
//                'title' => '系统消息',
//                'content' => $content,
//                'ctime' => date('Y-m-d H:i:s', time()),
//                'user_id' => $user_id
//            ]
//        )) {
//            DB::rollBack();
//
//            return ['status' => 2, 'message' => '发货失败'];
//        }
//        if (!Curl::PushNum($user_id)) {
//            DB::rollBack();
//
//            return ['status' => 2, 'message' => '发送系统消息失败'];
//        }
//        //推送
//        $jpush = Curl::PostCurl(Curl::CURL_ORDERPUSH,['alias' => $user_id, 'content' => $content]);
//        if ($jpush['code'] !== 200 || $jpush['msg']['responseCode'] !== 200) {
//            DB::rollBack();
//
//            return ['status' => 2, 'message' => '发货失败'];
//        }

        DB::commit();

        return ['status' => 1, 'message' => '发货成功'];
    }
}
